package com.nbsaas.boot;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class TimeRangeCalculator {

    public static void main(String[] args) {
        for (int i = 1; i < 6; i++) {
            List<Calendar> timeRange = getTimeRange(i);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Calendar calendar : timeRange) {
                System.out.println(dateFormat.format(calendar.getTime()));
            }
            System.out.println("**************************");
        }


        // 同样的方式可以应用于其他参数
    }

    public static List<Calendar> getTimeRange(int param) {
        Calendar now = Calendar.getInstance();
        List<Calendar> result = new ArrayList<>();

        switch (param) {
            case 1:
                result.add(getStartOfDay(now));
                result.add(getEndOfDay(now));
                break;
            case 2:
                result.add(getStartOfWeek(now));
                result.add(getEndOfWeek(now));
                break;
            case 3:
                result.add(getStartOfMonth(now));
                result.add(getEndOfMonth(now));
                break;
            case 4:
                result.add(getStartOfQuarter(now));
                result.add(getEndOfQuarter(now));
                break;
            case 5:
                result.add(getStartOfYear(now));
                result.add(getEndOfYear(now));
                break;
            default:
                // 处理无效参数的逻辑
                break;
        }

        return result;
    }

    private static Calendar getStartOfDay(Calendar calendar) {
        Calendar startOfDay = (Calendar) calendar.clone();
        startOfDay.set(Calendar.HOUR_OF_DAY, 0);
        startOfDay.set(Calendar.MINUTE, 0);
        startOfDay.set(Calendar.SECOND, 0);
        return startOfDay;
    }

    private static Calendar getEndOfDay(Calendar calendar) {
        Calendar endOfDay = (Calendar) calendar.clone();
        endOfDay.set(Calendar.HOUR_OF_DAY, 23);
        endOfDay.set(Calendar.MINUTE, 59);
        endOfDay.set(Calendar.SECOND, 59);
        return endOfDay;
    }

    private static Calendar getStartOfWeek(Calendar calendar) {
        Calendar startOfWeek = (Calendar) calendar.clone();
        startOfWeek.set(Calendar.DAY_OF_WEEK, startOfWeek.getFirstDayOfWeek());
        return getStartOfDay(startOfWeek);
    }

    private static Calendar getEndOfWeek(Calendar calendar) {
        Calendar endOfWeek = (Calendar) calendar.clone();
        endOfWeek.set(Calendar.DAY_OF_WEEK, endOfWeek.getActualMaximum(Calendar.DAY_OF_WEEK));
        return getEndOfDay(endOfWeek);
    }

    private static Calendar getStartOfMonth(Calendar calendar) {
        Calendar startOfMonth = (Calendar) calendar.clone();
        startOfMonth.set(Calendar.DAY_OF_MONTH, 1);
        return getStartOfDay(startOfMonth);
    }

    private static Calendar getEndOfMonth(Calendar calendar) {
        Calendar endOfMonth = (Calendar) calendar.clone();
        endOfMonth.set(Calendar.DAY_OF_MONTH, endOfMonth.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getEndOfDay(endOfMonth);
    }

    private static Calendar getStartOfQuarter(Calendar calendar) {
        Calendar startOfQuarter = (Calendar) calendar.clone();
        int currentMonth = startOfQuarter.get(Calendar.MONTH);
        int quarterStartMonth = (currentMonth / 3) * 3;
        startOfQuarter.set(Calendar.MONTH, quarterStartMonth);
        startOfQuarter.set(Calendar.DAY_OF_MONTH, 1);
        return getStartOfDay(startOfQuarter);
    }

    private static Calendar getEndOfQuarter(Calendar calendar) {
        Calendar endOfQuarter = (Calendar) calendar.clone();
        int currentMonth = endOfQuarter.get(Calendar.MONTH);
        int quarterEndMonth = ((currentMonth / 3) + 1) * 3 - 1;
        endOfQuarter.set(Calendar.MONTH, quarterEndMonth);
        endOfQuarter.set(Calendar.DAY_OF_MONTH, endOfQuarter.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getEndOfDay(endOfQuarter);
    }

    private static Calendar getStartOfYear(Calendar calendar) {
        Calendar startOfYear = (Calendar) calendar.clone();
        startOfYear.set(Calendar.DAY_OF_YEAR, 1);
        return getStartOfDay(startOfYear);
    }

    private static Calendar getEndOfYear(Calendar calendar) {
        Calendar endOfYear = (Calendar) calendar.clone();
        endOfYear.set(Calendar.DAY_OF_YEAR, endOfYear.getActualMaximum(Calendar.DAY_OF_YEAR));
        return getEndOfDay(endOfYear);
    }
}
